<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@page import="java.util.List" %>
<%@page import="com.liuya.entitle.ScriptTestResult" %>
<%@page import="com.liuya.entitle.QueryEntitlementTestResult" %>
<%@page import="com.liuya.safe.entitle.handler.QueryEntitlementHandler" %>
<%@ page import="com.liuya.common.I18N" %>

<%
    I18N i18n = I18N.getInstance();
    int first = Integer.parseInt(request.getParameter("first"));
    QueryEntitlementTestResult testResult = (QueryEntitlementTestResult) request.getAttribute("testResult");
    QueryEntitlementHandler handler = (QueryEntitlementHandler) request.getAttribute("handler");

%>

<% if (testResult.isFailed()) { %>
<font color="red">
    <%=i18n.say("Test_failed")%><br/>
    <pre><%=testResult.getErrorMessage() %></pre>
</font>
<% } else {
    String[] fields = (String[]) testResult.getQueryTestResult().getProperties();
    int totalCount = testResult.getQueryTestResult().getTotalRecords();
    String[][] testData = testResult.getQueryTestResult().getData();
%>
<font color="green"><b><%=i18n.say("Matched_query")%>: </b> <%=testResult.getMatchedQuery().getName() %>
</font>

<label><%=i18n.say("Total_records")%>: <%=totalCount %>
</label>
<% if (first != 0) { %>
<a href="javascript:showResult('<%=(first-15)<0?0:(first-15) %>')"><%=i18n.say("Previous")%>
</a>
<% } else { %>
&nbsp;&nbsp;&nbsp;
<% } %>
<% if (first + 15 < totalCount) { %>
<a href="javascript:showResult('<%=first+15%>')"><%=i18n.say("Next")%>
</a>
<% } %>
<table>
    <tr>
        <% for (int i = 0; i < fields.length; i++) {
        %>
        <th><%=fields[i] %>
        </th>
        <%
            }
        %>
    </tr>

    <% for (int i = 0; i < testData.length; i++) {
        String[] rowData = testData[i];
    %>
    <tr>
        <% for (int j = 0; j < rowData.length; j++) { %>
        <td><%=rowData[j] %>
        </td>
        <% } %>
    </tr>
    <% } %>
</table>

<% } %>

<script type="text/javascript">
    <% if( !testResult.isFailed() ) {
        // set yes column
    //	int matchedQueryId=testResult.getMatchedQuery().getId();

        //handler.getQueryEntitlements();
        List ucResults=testResult.getUserCategoryTestResults();
        for( int i=0,size=ucResults.size(); i<size; i++ ) {
            ScriptTestResult ucResult=(ScriptTestResult) ucResults.get( i );

            if( ucResult.isValid() ) {%>
    jQuery('#entitleTable tr:eq(<%=i+1%>) td:last').html('<img src="${contextPath}/style/images/yes.gif"/>');
    <%		} else { %>
    jQuery('#entitleTable tr:eq(<%=i+1%>) td:last').html('<img src="${contextPath}/style/images/fail.gif"/>');
    <%		}
        }
    }%>
</script>